﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication2.CLASS
{
   public class Order
    
    {
        #region " & PropertyS & "
        private string _OrderId;
        public string OrderId
        {
            get { return _OrderId; }
            set { _OrderId = value; }
        }
        private string _OrderAwsanOgnoo;
        public string OrderAwsanOgnoo
        {
            get { return _OrderAwsanOgnoo; }
            set { _OrderAwsanOgnoo = value; }
        }
        private string _OrderHvrgehOgnoo;
        public string OrderHvrgehOgnoo
        {
            get { return _OrderHvrgehOgnoo; }
            set { _OrderHvrgehOgnoo = value; }
        }
        private string _OrderStatus;
        public string OrderStatus
        {
            get { return _OrderStatus; }
            set { _OrderStatus = value; }
        }
        private string _ProductId;
        public string ProductId
        {
            get { return _ProductId; }
            set { _ProductId = value; }
        }
        private string _ProductToo;
        public string ProductToo
        {
            get { return _ProductToo; }
            set { _ProductToo = value; }
        }
        private string _XTolologchId;
        public string XTolologchId
        {
            get { return _XTolologchId;  }
            set { _XTolologchId = value; }
        }
        private string _CustomerId;
        public string CustomerId
        {
            get { return _CustomerId;  }
            set { _CustomerId = value; }
        }
        private string _TolborId;
        public string TolborId
        {
            get { return _TolborId; }
            set { _TolborId = value; }
        }
        #endregion
        public Order()
        {
            //butsets

        }
        public Order(string _OrderId, string _OrderAwsanOgnoo, string _OrderHvrgehOgnoo, string _OrderStatus, string _ProductId, string _ProductToo, string _XTolologchId, string _CustomerId, string _TolborId)
        {
            this.OrderId = _OrderId;
            this.OrderAwsanOgnoo = _OrderAwsanOgnoo;
            this.OrderHvrgehOgnoo = _OrderHvrgehOgnoo;
            this.OrderStatus = _OrderStatus;
            this.ProductId = _ProductId;
            this.ProductToo = _ProductToo;
            this.XTolologchId = _XTolologchId;
            this.CustomerId = _CustomerId;
            this.TolborId = _TolborId;
            
        }
        public static Order getbyRec(string _byOrderRec)
        {
            Order x = new Order();

            string qry1 = string.Format("select *from  Orders where OrderId=('{0}'", _byOrderRec);
            DataTable table = DBTools.selecQryTmp(qry1);

            try
            {
                return new Order(table.Rows[0]["OrderId"].ToString(), table.Rows[0]["OrderAwsanOgnoo"].ToString(), table.Rows[0]["OrderHvrgehOgnoo"].ToString(), table.Rows[0]["OrderStatusId"].ToString(), table.Rows[0]["ProductId"].ToString(), table.Rows[0]["ProductToo"].ToString(), table.Rows[0]["XTolologchId"].ToString(), table.Rows[0]["CustomerId"].ToString(), table.Rows[0]["TolborId"].ToString());


            }
            catch (Exception e)
            {
                //MsgBox(ex.Message, MsgBoxStyle.OkCancel, "account->getAccountByRecID")'
                MessageBox.Show(string.Format("{0} Exception caught.", e));

                return null;
            }



        }
        public static bool AddFunction(string _OrderId, string _OrderAwsanOgnoo, string _OrderHvrgehOgnoo, string _OrderStatus, string _ProductId, string _ProductToo, string _XTolologchId, string _CustomerId, string _TolborId)
        {

            //DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureOrders";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderId", SqlDbType.NVarChar, 8)).Value = _OrderId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderAwsanOgnoo", SqlDbType.Date)).Value = _OrderAwsanOgnoo;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderHvrgehOgnoo", SqlDbType.Date)).Value = _OrderHvrgehOgnoo;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderStatusId", SqlDbType.NVarChar, 15)).Value = _OrderStatus;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductId", SqlDbType.NVarChar, 8)).Value = _ProductId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductToo", SqlDbType.Int)).Value = _ProductToo;
            DBTools._Comm.Parameters.Add(new SqlParameter("@XTolologchId", SqlDbType.NVarChar, 8)).Value = _XTolologchId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerId", SqlDbType.NVarChar, 8)).Value = _CustomerId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@TolborId", SqlDbType.NVarChar, 10)).Value = _TolborId;


            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "ADD";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                try
                {
                    int rowcount = DBTools._Comm.ExecuteNonQuery();
                }
                catch(Exception e)
                {
                    
                }
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                MessageBox.Show("Тоо ширхэгийг оруулна уу !!!");


            }

            return false;


        }
        public static bool EditFunction(string _byOrderId, string _byOrderAwsanOgnoo, string _byOrderHvrgehOgnoo, string _byOrderStatus, string _byProductId, string _byProductToo, string _byXTolologchId, string _byCustomerId, string _byTolborId)
        {


           // DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureOrders";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderId", SqlDbType.NVarChar, 8)).Value = _byOrderId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderAwsanOgnoo", SqlDbType.Date)).Value = _byOrderAwsanOgnoo;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderHvrgehOgnoo", SqlDbType.Date)).Value = _byOrderHvrgehOgnoo;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderStatusId", SqlDbType.NVarChar, 15)).Value = _byOrderStatus;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductId", SqlDbType.NVarChar, 8)).Value = _byProductId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductToo", SqlDbType.Int)).Value = _byProductToo;
            DBTools._Comm.Parameters.Add(new SqlParameter("@XTolologchId", SqlDbType.NVarChar, 8)).Value = _byXTolologchId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerId", SqlDbType.NVarChar, 8)).Value = _byCustomerId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@TolborId", SqlDbType.NVarChar, 10)).Value = _byTolborId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "EDIT";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;




        }

        public static bool DelFunction(string _byOrderId)
        {


            //DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureOrders";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderId", SqlDbType.NVarChar, 8)).Value = _byOrderId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderAwsanOgnoo", SqlDbType.Date)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderHvrgehOgnoo", SqlDbType.Date)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@OrderStatusId", SqlDbType.NVarChar, 15)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductId", SqlDbType.NVarChar, 8)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@ProductToo", SqlDbType.Int)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@XTolologchId", SqlDbType.NVarChar, 8)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerId", SqlDbType.NVarChar, 8)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@TolborId", SqlDbType.NVarChar, 10)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "DEL";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            } 

            return false;

       



        }
    }
}
